package com.tj.collection.db.service.impl;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.StringUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.kly.user.dto.chatter.Chatter;
import com.tj.collection.db.dao.ChatterWorkSettingDao;
import com.tj.collection.db.dbo.ChatterWorkSettingDBO;
import com.tj.collection.db.domain.ChatterWorkingSet;
import com.tj.collection.db.entity.ChatterWorkSettingEntity;
import com.tj.collection.db.service.ChatterWorkingSetService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;


@Service("chatterWorkingSetService")
public class ChatterWorkingSetServiceImpl implements ChatterWorkingSetService {

    @Resource
    ChatterWorkSettingDao chatterWorkSettingDao;


    @Override
    public ChatterWorkingSet getChatterWorkingSet(String chatterCode) {
        if (StringUtils.isEmpty(chatterCode)) {
            return null;
        }

        final ChatterWorkSettingEntity chatterWorkingSet = chatterWorkSettingDao.getChatterWorkingSet(chatterCode);
        if (Objects.isNull(chatterWorkingSet)) {
            return null;
        }

        ChatterWorkingSet dto = new ChatterWorkingSet();
        BeanUtils.copyProperties(chatterWorkingSet, dto);

        return dto;
    }

    @Override
    public List<ChatterWorkingSet> listChatterWorkingSet(List<Chatter> chatters) {
        if (CollectionUtils.isEmpty(chatters)) {
            return Lists.newArrayList();
        }
        final List<String> chatterCodes = chatters.parallelStream().map(Chatter::getChatterCode).collect(Collectors.toList());

        final List<ChatterWorkSettingDBO> chatterWorkSettingEntities = chatterWorkSettingDao.listAllChatterWorkingSet();
        if (CollectionUtils.isEmpty(chatterWorkSettingEntities)) {
            return Lists.newArrayList();
        }

        return chatterWorkSettingEntities.parallelStream().filter(o -> chatterCodes.contains(o.getUserCode())).map(o -> {
            ChatterWorkingSet chatterWorkingSet = new ChatterWorkingSet();
            BeanUtils.copyProperties(o, chatterWorkingSet);
            return chatterWorkingSet;
        }).collect(Collectors.toList());
    }

    @Override
    public Map<String, ChatterWorkingSet> getChatterWorkingSetMap(List<Chatter> chatters) {
        if (CollectionUtils.isEmpty(chatters)) {
            return Maps.newHashMap();
        }
        final List<String> chatterCodes = chatters.parallelStream().map(Chatter::getChatterCode).collect(Collectors.toList());

        final List<ChatterWorkSettingEntity> chatterWorkSettingEntities = chatterWorkSettingDao.listChatterWorkingSetByChatterCode(chatterCodes);
        if (CollectionUtils.isEmpty(chatterWorkSettingEntities)) {
            return Maps.newHashMap();
        }

        return chatterWorkSettingEntities.parallelStream().map(o -> {
            ChatterWorkingSet chatterWorkingSet = new ChatterWorkingSet();
            BeanUtils.copyProperties(o, chatterWorkingSet);
            chatterWorkingSet.setUserCode(o.getChatterCode());
            return chatterWorkingSet;
        }).collect(Collectors.toMap(ChatterWorkingSet::getUserCode, o -> o));


    }
}
